package com.rtos.vo.file.refresh;

import com.rtos.model.fileSystem.FileUtil;
import com.rtos.vo.file.diskPane.DiskViewer;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;

import java.util.ArrayList;

public class Refresh {
    public TreeView FileTree;
    public FileUtil fileUtil;
    public DiskViewer diskViewer;
    public Refresh(TreeView fileTree, FileUtil fileUtil,DiskViewer diskViewer) {
        FileTree = fileTree;
        this.fileUtil = fileUtil;
        this.diskViewer=diskViewer;
        this.diskViewer.updateStatus();
        System.out.println("磁盘刷新");
    }
    public void refreshFiles(){
        TreeItem<String> root =new TreeItem<>("root");
        FileTree.setRoot(root);
        fileScnner("root",root);
        root.setExpanded(true);
    }
    public void fileScnner(String nowPath,TreeItem PathItem){
        ArrayList<String> files = new ArrayList<String>();
        files = fileUtil.getDirectorysSimplified(nowPath);
//        System.out.println("myprint:");
        for (String file : files) {
            // file = file.replace("/","");
            if(file.contains(".")){
                //文件
                int index = file.lastIndexOf("/");
                String fileItemNext = file.substring(index+1);
                TreeItem<String> fileItem =new TreeItem<>(fileItemNext);
                System.out.println(fileItemNext);
                PathItem.getChildren().add(fileItem);
            }else{
                System.out.println(file);
//                String s = null;
                int index = file.lastIndexOf("/");
                String fileItemNext = file.substring(index+1);
                TreeItem<String> fileItem =new TreeItem<>(fileItemNext);
                PathItem.getChildren().add(fileItem);
                System.out.println(fileItemNext);
                fileScnner(file,fileItem);
            }
        }
    }

}
